package config

import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"log"
)

type CaptchaResult struct {
	Id           string `json:"id"`
	Base64Blob   string `json:"base_64_blob"`
	VertifyValue string `json:"code"`
}

//// 验证码默认存储对象，数量10240，有效时间10分钟
//var store = base64Captcha.DefaultMemStore

// Math 配置参数
var (
	Length   = 5
	Width    = 60
	Height   = 30
	MaxSkew  = 0.25
	DotCount = 18
)

//生成图形化验证码
func GenerateCaptcha(c *gin.Context) *CaptchaResult {
	//配置RedisStore RedisStore实现base64Captcha.Store的接口
	var store base64Captcha.Store = RediStore

	// 这里用的是类型的验证码
	driver := base64Captcha.NewDriverDigit(Height, Width, Length, MaxSkew, DotCount)
	captcha := base64Captcha.NewCaptcha(driver, store)
	id, b64s, err := captcha.Generate()
	if err != nil {
		//c.JSON(http.StatusBadRequest, gin.H{
		//	"msg": "验证码获取失败",
		//})
		log.Fatal("验证码获取失败")
		return nil
	}
	//c.JSON(http.StatusOK, gin.H{
	//	"captchaId": id,
	//	"captcha":   b64s,
	//})

	captchaResult := CaptchaResult{Id: id, Base64Blob: b64s}

	return &captchaResult

}

func VertifyCaptcha(id string, value string) bool {
	vertifyResult := RediStore.Verify(id, value, true)
	return vertifyResult
}
